// my/wx.js
import {
  RechargeModel
} from '../../models/recharge'
import {
  Format
} from '../../utils/common'
const rechargeModel = new RechargeModel()
const app = getApp()
let last
const format = new Format()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recharge: {},
    startTime: null,
    endTime: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.user.phone) {
      this.setData({
        inputDisabled: false,
      });
      this.setData({
        'recharge[phone]': app.globalData.user.phone,
        'recharge[cardAsn]':app.globalData.user.cardAsn
      });
    }
    console.log(app.globalData)
    console.log(options)
    if (options.endTime) {
      this.setData({
        startTime: options.startTime,
          endTime: options.endTime
      })
    }
    this.loadOrders()
    // app.myLogin().then(res => {
    //   console.log('myLogin' + JSON.stringify(res))
    //   // this.loadUserInfo()
    //   console.log('data' + JSON.stringify(app.globalData.user) )
    //   if (res.phone) {
    //     this.setData({
    //       inputDisabled: false,
    //     });
    //     this.setData({
    //       'recharge[phone]': res.phone,

    //     });
    //     console.log('data' + JSON.stringify(this.data.recharge) )
    //   }
    // })
  },
  loadOrders() {
    // const startTime= this.data.startTime
    // const endTime = this.data.endTime
    var startTime= this.data.startTime
    var endTime = this.data.endTime
    if (!startTime && !endTime) {
      var myDate = new Date();
      var tYear = myDate.getFullYear();
      var tMonth = myDate.getMonth();
      startTime = format.date("yyyy-MM", new Date()) + '-01'
      endTime = format.date("yyyy-MM", new Date()) + '-31'
    }
    rechargeModel.tradeOrderListQuery(startTime, endTime,2).then(res => {
      this.setData({
        orders: res
      })
    })
  },

  tip(e) {
    const page = e.currentTarget.dataset.url
    if (page =='/pages/recharge/recharge'){
      wx.redirectTo({
        url: page
      })
      return
    }
    if (!app.globalData.user.phone) {
      wx.getPhoneNumber({
        success: (res) => {
            let encryptedData = res.response;
            app.myoilLogin(encryptedData).then(res => {
              wx.hideLoading();
              console.log('myoilLogin------' + JSON.stringify(res))
              if (res.result == 500) {
                wx.showToast({
                  type: 'none',
                  content: res.message,
                  duration: 1000,
                });
                return
              }
              const shopId = this.data.station.shopId
              this.data.inputDisabled = false
            })
        },
        fail: (res) => {
            console.log(res);
            console.log('getPhoneNumber_fail');
            wx.showToast({
              type: 'none',
              content: 'getPhoneNumber~失败',
              duration: 1000,
            });
        },
      });
      
    } else {
      
    }
  },
  goCalendar() {
    wx.redirectTo({
      url: '/pages/calendar/calendar?url=rechargeOrder'
    })
  },
  loadUserInfo() {
    app.getUserInfo().then(
      user => {
        this.setData({
          user,
        });
      },
      () => {
        // 获取用户信息失败
      }
    );
  },
  loadMyAccount() {
    rechargeModel.loadMyAccount().then(res => {
      console.log(res)
      wx.stationName = '999999'
      this.setData({
        my: res
      })
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.loadMyAccount()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})